require "Common/define"
require "Logic/Config"
require "Common/functions"

UIDrawProbability = {};
HelperFunc.SetUIEnv(UIDrawProbability)
local this = UIDrawProbability;

this.btn_Close = nil;
this.but_Back = nil;

this.hasInit = false;

this.curPoolId = 0;
this.tableContent={}

this.dynamicList = nil;


-- this.ComfirmDraw = 999;

function UIDrawProbability.Init(data)
    curPoolId = data;
    local name, packageName = getUIName(PanelNames.UIDrawProbability);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
    -- this.coStart= coroutine.start(this.onUpdate)
end

function UIDrawProbability.onCreate(obj)
    if not hasInit then
        FireEvent(Config.EventType.Close_headMenu,false);
        hasInit = true;
        -- ComfirmDraw = 999;
        btn_Close = obj:GetChild("BG");
        but_Back = obj:GetChild("but_back");
     
        dynamicList = obj:GetChild("Content")-- :GetComponent("DynamicHeightList");
        this.addEvent();
    end
    this.updateContentNew();
    -- if (tonumber(curPoolId) >= 4 and ComfirmDraw ~= 999) then
    --     this.onClickChangeProfession(ComfirmDraw)
    -- end
    
end


function UIDrawProbability.addEvent()
    but_Back.onClick:Add(this.onClose);
    btn_Close.onClick:Add(this.onClose);
end


function UIDrawProbability.removeEvent()
    but_Back.onClick:Remove(this.onClose);
    btn_Close.onClick:Remove(this.onClose);
end

function UIDrawProbability.OnDestroy()
    this.removeEvent();
end

function UIDrawProbability.OnEnable()
end

function UIDrawProbability.OnDisable()
    coroutine.stop(this.coStart)
end

function UIDrawProbability.onClose()
    FireEvent(Config.EventType.Close_headMenu,true);
    closeUI(PanelNames.UIDrawProbability);
  
end

--刷新卡池内容
function UIDrawProbability.updateContentNew()
    local poolcfg = G.dataTable["t_drawpool"][tostring(curPoolId)];
    if poolcfg == nil then
        return
    end
  
    tableContent = {}
    local items = decodeJsonStrToTabel(poolcfg.f_ShowProbability);
    local heroItems = decodeJsonStrToTabel(poolcfg.f_ShowHero);
    local itemNum = #items;
    if itemNum ~= #heroItems then
        UISysTips.AddMsg("概率配置与展示英雄 数量不一致");
        return;
    end

    local tip={}
    tip.isUp =false;
    tip.isTip = true;
    --tip.content =getLanguage("HelpDes36");
    local dataTable = G.dataTable["t_drawpool"][tostring(curPoolId)];
    tip.content =getLanguage(dataTable.f_DrawPoolDesc);
    table.insert(tableContent,tip)
    
    local upShowItems = decodeJsonStrToTabel(poolcfg.f_UpShow);
    local upShowItemNum =0
    if upShowItems~=nil and #upShowItems>0 then
    
        itemNum = itemNum + 1
        local v={}
        v.isUp = true
        v.isTip=false;
        v.quality=0;
        v.content = upShowItems
        table.insert(tableContent,v)
    end
  

    for i=1, #items do
        local v={}
        v.isUp=false
        v.isTip=false;
        v.probability = items[i]
        v.quality=i;
        v.content = heroItems[i]
        table.insert(tableContent,v)
    end

    itemNum = itemNum +1;
    --dynamicList.numItems = 0
    dynamicList.itemRenderer=this.setData;
    --dynamicList:SetVirtual()
    dynamicList.numItems = itemNum
    --HelperFunc.ListTranstionPlay(dynamicList)
    --dynamicList:ScrollToView(0);
end

function UIDrawProbability.setData(index, itemobj)
    local itemVal = tableContent[index+1];
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.Btn_DrowProbablityItem);
    if ctrlItem ~= nil then
        ctrlItem.initData(itemobj,itemVal );
    end
end

function UIDrawProbability.receiveOpenPoolMsg(msg)
    UISysTips.AddMsg("解锁成功");
    UIDraw.OpenState[msg.poolId].open = 1;
    UIDraw.OpenState[msg.poolId].closeTime = msg.closeTime;
    -- this.UnlockTime();
    this.updateContentNew();
end

return UIDrawProbability;